import React,{useState,useEffect} from 'react';
import {Table,Button,Modal,notification,Tag} from 'antd'
import axios from '_axios@0.27.2@axios';
import { FileDoneOutlined,DeleteOutlined,ExclamationCircleOutlined } from '@ant-design/icons';
const Audit = (props) => {
  const [dataSource, setdataSource] = useState([])
  const columns = [
     {
      title: '新闻标题',
      dataIndex: 'title',
      render: (title,item) => {
        return <a href={`#/news-manage/preview/${item.id}`}>{title}</a>
      }
    },
    {
      title: '作者',
      dataIndex: 'author'
    },
    {
      title: '新闻分类',
      dataIndex: 'category',
      render: (category) => {
        return category.title
      }
    },
    {
      title: '操作',
      render: (item) => {
        return <div>
          <Button
            style={{marginRight:'5px'}}
             onClick={() => handleReject(item,3,0)}
           >驳回</Button>
         <Button
             type="primary"
             icon={<FileDoneOutlined />}
             onClick={() =>  handleReject(item,2,1)}
           >同意</Button>
       </div>
      }
    }
  ]
  // 同意--驳回
  const handleReject = (item,auditState,publishState) => {
    setdataSource(dataSource.filter(data => data.id !== item.id))
    axios.patch(`/news/${item.id}`,{
      auditState,
      publishState
    }).then(res => {
       notification.info({
        message: `通知`,
        description:`您可以在【审核管理/审核列表】中查看您的新闻审核状态`,
        placement:'bottomRight'
      });
    })
  }
  useEffect(() => {
    const {username,roleId,region} = JSON.parse(localStorage.getItem('token'))
    axios.get(`/news?auditState=1&_expand=category`).then(res => {
      setdataSource(roleId === 1 ? res.data : [
        ...res.data.filter(item => item.author === username),
        ...res.data.filter(item=>item.region===region && roleId===3)
      ])
    })
  }, [])
  return (
    <div>
      <Table
        dataSource={dataSource}
        columns={columns}
        pagination={{
          pageSize: 5
        }}
        rowKey={(item)=>item.id}
      ></Table>
    </div>
  );
}

export default Audit;


